﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace Aribaccio.Copyright.Client
{
    public partial class MainWindowModel : ObservableModel
    {
        /// <summary>
        /// 是否已初始化加载
        /// </summary>
        protected bool IsLoaded { get; set; }
        /// <summary>
        /// 注册版权期限实例
        /// </summary>
        protected IRegisterCopyLimits CopyLimits { get; set; }

        /// <summary>
        /// 是否允许启用注册
        /// </summary>
        [ObservableProperty]
        protected bool isEnabledRegedit = true;
        /// <summary>
        /// 期限剩余天数
        /// </summary>
        [ObservableProperty]
        protected int limitSurplusDays;
        /// <summary>
        /// 机器码
        /// </summary>
        [ObservableProperty]
        protected string machineCode = string.Empty;
        /// <summary>
        /// 注册码
        /// </summary>
        [ObservableProperty]
        protected string regeditCode = string.Empty;
        /// <summary>
        /// 填充状态消息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 填充状态画刷颜色
        /// </summary>
        [ObservableProperty]
        protected Brush statusForeground = new SolidColorBrush(Colors.White);

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindowModel(IRegisterCopyLimits argRegisterCopyLimits)
        {
            this.CopyLimits = argRegisterCopyLimits;
        }

        /// <summary>
        /// 初始化加载事件
        /// </summary>
        [RelayCommand]
        public void ExecuteLoaded()
        {
            if (this.IsLoaded) return;
            //初始化加载机器码
            this.MachineCode = this.CopyLimits.ReadMachineCode();
            //初始化检查注册序列号及期限
            IRegisterCopyMsg ResultMsg = this.CopyLimits.CheckRegisterCopyLimits();
            this.LimitSurplusDays = ResultMsg.LimitSurplusDays;
            switch (ResultMsg.Status)
            {
                default:
                case RegisterCopyStatus.None:
                    this.SetStatusMsg(ResultMsg.StatusMsg, Colors.Red);
                    break;
                case RegisterCopyStatus.OK:
                    this.SetStatusMsg(ResultMsg.StatusMsg, Colors.Green);
                    break;
                case RegisterCopyStatus.OKRegister:
                    this.SetStatusMsg(ResultMsg.StatusMsg, Colors.Red);
                    break;
                case RegisterCopyStatus.OKTimeLimits:
                    this.SetStatusMsg(ResultMsg.StatusMsg, Colors.Green);
                    break;
            }
            this.IsLoaded = true;
        }

        /// <summary>
        /// 复制机器码命令事件
        /// </summary>
        [RelayCommand]
        public void ExecuteClipboard()
        {
            if (string.IsNullOrWhiteSpace(this.MachineCode))
            {
                this.SetStatusMsg("机器码未生成！", Colors.Red);
                return;
            }
            Clipboard.SetText(this.MachineCode);
            this.SetStatusMsg("机器码复制成功", Colors.Green);
        }

        /// <summary>
        /// 注册命令事件
        /// </summary>
        [RelayCommand]
        public void ExecuteRegedit()
        {
            if (!this.IsEnabledRegedit) return;
            try
            {
                this.IsEnabledRegedit = false;
                if (string.IsNullOrWhiteSpace(this.RegeditCode))
                {
                    this.SetStatusMsg("请输入注册码！", Colors.Red);
                    return;
                }
                IRegisterCopyMsg ResultMsg = this.CopyLimits.WriteSerialNum(this.RegeditCode.Trim());
                this.LimitSurplusDays = ResultMsg.LimitSurplusDays;
                if (ResultMsg.Status == RegisterCopyStatus.OK)
                    this.SetStatusMsg("注册成功", Colors.Green);
                else
                    this.SetStatusMsg(ResultMsg.StatusMsg, Colors.Red);
            }
            finally
            {
                this.IsEnabledRegedit = true;
            }
        }

        /// <summary>
        /// 设置状态信息
        /// </summary>
        protected void SetStatusMsg(string argMsg, Color argColor)
        {
            this.StatusMsg = argMsg;
            this.StatusForeground = new SolidColorBrush(argColor);
        }
    }
}
